package com.estronger.shopmanage.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.utils.DensityUtils;
import com.estronger.shopmanage.utils.PrefUtils;

import java.util.ArrayList;

public class GuideActivity extends BaseActivity implements OnClickListener {

	private static final int[] mImageIds = new int[] { R.drawable.default_1,
			R.drawable.default_2, R.drawable.default_3 };
	private ArrayList<ImageView> mImageViewList;
	private ViewPager vpGuide;
	private LinearLayout ll_PointGroup;
	private int mPointWidth;
	private View viewRedPoint;
	private TextView tv_guide_start;

	/**
	 * 新手引导页初始化布局和控件
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);

		vpGuide = (ViewPager) findViewById(R.id.vp_guide);
		ll_PointGroup = (LinearLayout) findViewById(R.id.ll_point_group);
		viewRedPoint = findViewById(R.id.view_red_point);
		tv_guide_start = (TextView) findViewById(R.id.guide_start);
		tv_guide_start.setOnClickListener(this);

		vpGuide.setAdapter(new GuideAdapter());
		vpGuide.setOnPageChangeListener(new GuidePageListener());
		initViews();

		mImageViewList = new ArrayList<ImageView>();

		for (int i = 0; i < mImageIds.length; i++) {
			ImageView image = new ImageView(this);
			image.setBackgroundResource(mImageIds[i]);
			image.setScaleType(ImageView.ScaleType.CENTER_CROP);
			mImageViewList.add(image);
			if (i == (mImageIds.length - 1)) {
				image.setOnClickListener(this);
			}
		}
	}

	private void initViews() {

		for (int i = 0; i < 3; i++) {
			View point = new View(this);
			point.setBackgroundResource(R.drawable.shape_point_gray);

			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					DensityUtils.dp2px(this, 10), DensityUtils.dp2px(this, 10));
			if (i > 0) {
				params.leftMargin = DensityUtils.dp2px(this, 10) * 2;
			}

			point.setLayoutParams(params);

			ll_PointGroup.addView(point);
		}

		ll_PointGroup.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						ll_PointGroup.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
						mPointWidth = ll_PointGroup.getChildAt(1).getLeft()
								- ll_PointGroup.getChildAt(0).getLeft();

					}
				});
	}

	class GuideAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public ImageView instantiateItem(ViewGroup container, int position) {
			container.addView(mImageViewList.get(position));
			return mImageViewList.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}

	class GuidePageListener implements OnPageChangeListener {

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {

			int len = (int) (mPointWidth * positionOffset) + position
					* mPointWidth;

			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewRedPoint
					.getLayoutParams();
			params.leftMargin = len;

			viewRedPoint.setLayoutParams(params);
		}

		@Override
		public void onPageSelected(int position) {

			if (position == 2) {
				tv_guide_start.setText("进入");
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {

		}

	}

	@Override
	public void onClick(View v) {
		PrefUtils.setBoolean(GuideActivity.this, "is_user_guide_showed", true);
		startActivity(new Intent(GuideActivity.this, ServerActivity.class));
		finish();
	}

}
